package com.bedrock.project.inside.service.impl;

import com.bedrock.common.utils.DateUtils;
import com.bedrock.common.utils.IdWord;
import com.bedrock.framework.security.LoginUser;
import com.bedrock.project.inside.entity.Interviewer;
import com.bedrock.project.inside.entity.WaitAffair;
import lombok.extern.flogger.Flogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bedrock.project.inside.mapper.InterviewerMapper;
import com.bedrock.project.inside.service.IInterviewerService;

import java.util.*;

/**
 * 面试官池Service业务层处理
 * 
 * @author bedrock
 * @date 2020-04-08
 */
@Slf4j
@Service
public class InterviewerServiceImpl implements IInterviewerService 
{
    @Autowired
    private InterviewerMapper interviewerMapper;

    /**
     * 查询面试官池
     * 
     * @param id 面试官池ID
     * @return 面试官池
     */
    @Override
    public Interviewer selectInterviewerById(Long id)
    {
        return interviewerMapper.selectInterviewerById(id);
    }

    /**
     * 查询面试官池列表
     * 
     * @param interviewer 面试官池
     * @return 面试官池
     */
    @Override
    public List<Interviewer> selectInterviewerList(Interviewer interviewer)
    {
        return interviewerMapper.selectInterviewerList(interviewer);
    }

    /**
     * 新增面试官池
     * 
     * @param interviewer 面试官池
     * @return 结果
     */
    @Override
    public int insertInterviewer(Interviewer interviewer)
    {
        interviewer.setCreateTime(DateUtils.getNowDate());
        return interviewerMapper.insertInterviewer(interviewer);
    }

    /**
     * 批量新增面试官池
     *
     * @param userIds 面试官池
     * @return 结果
     */
    @Override
    public String insertInterviewerList(List<Long> userIds,Long createBy){

        if(userIds.size()==0){
            return "新选择面试官";
        }
        String result ="";
        // 已经存在的面试官
        List<Long> inList =  interviewerMapper.findInterViewUserByUserIdList(userIds);
        Map<String,Object> map = new HashMap<String,Object>(10);
        userIds.forEach(x->{
            map.put(x.toString(),"value");
        });
        List<Long> addList = new ArrayList<>();


        for(int i=0;i<userIds.size();i++){
            boolean isExists = false;
            for(int j=0;j<inList.size();j++){
                if(userIds.get(i).equals(inList.get(j))){
                    // 找到相等，用户已存在
                    isExists = true;
                    break;
                }

            }

            if(!isExists){
                addList.add(userIds.get(i));
            }
        }

        // 小于0，或者不与之前传入的相等，表示可能存在有新增的，也有之前就存过的，这个情况不做保存
        if(addList.size()<=0){
            return "没有新增的面试官或已存在，请重新选择";
        }
        if(addList.size()!=userIds.size()){
            return "选择的面试官已存在，请重新选择";
        }
        // 查询新增面试官的信息
        List<Interviewer> viewerList = interviewerMapper.selectInterViewUserListByIds(addList);
        if(viewerList.size()<=0){
            return "新增面试官信息查询不到";
        }
        int num = 0;
        for(int i=0;i<viewerList.size();i++){
            Interviewer view = viewerList.get(i);
            view.setId(IdWord.getId());
            // 墨认空闲
            view.setViewStatic(1);
            view.setCreateBy(String.valueOf(createBy));
            view.setCreateTime(new Date());
            view.setUpdateTime(new Date());
            interviewerMapper.insertInterviewer(view);
            num++;
        }

        return  "新增面试官成功";
    }

    /**
     * 修改面试官池
     * 
     * @param interviewer 面试官池
     * @return 结果
     */
    @Override
    public int updateInterviewer(Interviewer interviewer)
    {
        interviewer.setUpdateTime(DateUtils.getNowDate());
        return interviewerMapper.updateInterviewer(interviewer);
    }

    /**
     * 批量删除面试官池
     * 
     * @param ids 需要删除的面试官池ID
     * @return 结果
     */
    @Override
    public int deleteInterviewerByIds(Long[] ids)
    {
        // 查询面试官是否正在面试中

        return interviewerMapper.deleteInterviewerByIds(ids);
    }

    /**
     * 删除面试官池信息
     * 
     * @param id 面试官池ID
     * @return 结果
     */
    @Override
    public int deleteInterviewerById(Long id)
    {
        return interviewerMapper.deleteInterviewerById(id);
    }


    @Override
    public boolean deleteInterviewerByUserId(List<Long> userIds){
        try{
            interviewerMapper.deleteInterviewerByUserId(userIds);
        }catch (Exception ex){
            ex.printStackTrace();
            return false;
        }

        return true;
    }




}
